import { MockMethod } from "vite-plugin-mock";
import { resultError, resultSuccess } from "../_utils";
import { createToken, verifyToken } from "../auth";

// 模拟刷新 access token 的接口（实际上一般会连着 refresh token 也刷新一下）
export default [
    {
        url: "/refreshtoken",
        method: "get",
        response: ({ headers }) => {
            if (verifyToken(headers.authorization)) {
                const data = {
                    accessToken: createToken({ name: "accessToken" }, 10),
                    refreshToken: createToken({ name: "refreshToken" }, 200)
                };
                return resultSuccess(data, { message: "refresh token successfully" });
            } else {
                return resultError("refresh token 无效", { code: 401 });
            }
        }
    }
] as MockMethod[];
